import os
from PIL import Image, ImageOps

def resize_image(image_path, width, height):
    try:
        image = Image.open(image_path)
    except IOError, e:
        print e
        pass
    # save original
    path, ext = os.path.splitext(image_path)
    image.save(path + '.orig' + ext, image.format)
    # Convert to RGB if necessary
    # Thanks to Limodou on DjangoSnippets.org
    # http://www.djangosnippets.org/snippets/20/
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    # resize and save
    image = ImageOps.fit(image, (width, height), method=Image.ANTIALIAS, 
            centering=(0.5, 0))
    image.save(image_path, image.format, quality=75)

